package zzs.generalframework.console.core.parameter.commonparameter.dictionary.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import zzs.generalframework.common.core.parameter.commonparameter.dictionary.DictionaryBO;
import zzs.generalframework.common.core.parameter.commonparameter.dictionary.DictionaryInfo;
import zzs.generalframework.common.core.parameter.commonparameter.dictionary.DictionaryPO;
import zzs.generalframework.console.common.exception.ConsoleException;
import zzs.generalframework.console.common.exception.ConsoleRollbackException;
import zzs.generalframework.console.common.model.ConsoleReturnBO;
import zzs.generalframework.console.common.model.ConsoleReturnBO.ConsoleResult;
import zzs.generalframework.console.core.parameter.commonparameter.dictionary.dao.IDictionaryDao;
import zzs.generalframework.console.core.parameter.commonparameter.dictionary.service.IDictionaryService;
import zzs.generalframework.util.core.reflect.BeanUtils;

/**
 * @title
 * @description
 * @project zzs-console
 * @package zzs.generalframework.console.core.parameter.commonparameter.dictionary.service.impl
 * @author zzs
 * @date 2018年2月2日
 * @version 1.0
 */
@Service("zzs.generalframework.console.core.parameter.commonparameter.dictionary.service.impl.dictionaryServiceImpl")
public class DictionaryServiceImpl implements IDictionaryService
{

	private Logger logger = LoggerFactory.getLogger(DictionaryServiceImpl.class);
	@Autowired
	BeanUtils beanUtils;
	@Autowired
	IDictionaryDao dictionaryDao;

	@Override
	public ConsoleReturnBO saveDictionary(DictionaryBO dictionaryBO)
	{
		logger.debug("====保存数据字典信息开始====");
		logger.debug("====保存数据字典信息.入参:DictionaryBO=[{}]====", dictionaryBO);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		DictionaryPO dictionaryPO = new DictionaryPO();
		copyDictionaryBO2DictionaryPO(dictionaryBO, dictionaryPO);
		Boolean insertResult = dictionaryDao.insert(dictionaryPO);
		if (insertResult)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		}
		logger.debug("====保存数据字典信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====保存数据字典信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO saveDictionary(List<DictionaryBO> dictionaryBOList) throws ConsoleException
	{
		logger.info("====保存数据字典信息列表开始====");
		logger.debug("====保存数据字典信息列表.入参:DictionaryBOList=[{}]====", dictionaryBOList);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		for (DictionaryBO itemBO : dictionaryBOList)
		{
			ConsoleReturnBO itemResult = saveDictionary(itemBO);
			if (ConsoleResult.FAILD.equals(itemResult.getResult()))
			{
				returnConsoleReturnBO.setResult(ConsoleResult.FAILD);
				returnConsoleReturnBO.setMessage(itemResult.getMessage());
				throw new ConsoleRollbackException();
			}
		}
		logger.debug("====保存数据字典信息列表.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====保存数据字典信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO getDictionary(DictionaryInfo dictionaryInfo)
	{
		logger.info("====获取数据字典信息开始====");
		logger.debug("====获取数据字典信息.入参:DictionaryInfo=[{}]====", dictionaryInfo);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		List<DictionaryPO> dictionaryPOList = dictionaryDao.select(dictionaryInfo);
		if (dictionaryPOList == null || dictionaryPOList.size() == 0)
		{
			returnConsoleReturnBO.setMessage("未获取到记录");
		}
		else if (dictionaryPOList.size() == 1)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
			DictionaryBO dictionaryBO = new DictionaryBO();
			copyDictionaryPO2DictionaryBO(dictionaryPOList.get(0), dictionaryBO);
			returnConsoleReturnBO.setReturnObj(dictionaryBO);
		}
		else
		{
			returnConsoleReturnBO.setMessage("记录不唯一");
			DictionaryBO dictionaryBO = new DictionaryBO();
			copyDictionaryPO2DictionaryBO(dictionaryPOList.get(0), dictionaryBO);
			returnConsoleReturnBO.setReturnObj(dictionaryBO);
		}
		logger.debug("====获取数据字典信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====获取数据字典信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO listDictionary(DictionaryInfo dictionaryInfo)
	{
		logger.info("====获取数据字典信息列表开始====");
		logger.debug("====获取数据字典信息列表.入参:DictionaryInfo=[{}]====", dictionaryInfo);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		List<DictionaryPO> dictionaryPOList = dictionaryDao.select(dictionaryInfo);
		if (dictionaryPOList == null || dictionaryPOList.size() == 0)
		{
			returnConsoleReturnBO.setMessage("未获取到记录");
		}
		else
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
			List<DictionaryBO> resultList = new ArrayList<DictionaryBO>();
			for (DictionaryPO itemPO : dictionaryPOList)
			{
				DictionaryBO itemBO = new DictionaryBO();
				copyDictionaryPO2DictionaryBO(itemPO, itemBO);
				resultList.add(itemBO);
			}
			returnConsoleReturnBO.setReturnObj(resultList);
		}
		logger.debug("====获取数据字典信息列表.出参:ConsoleReturnBO＝[{}]====", returnConsoleReturnBO);
		logger.info("====获取数据字典信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO updateDictionary(DictionaryBO dictionaryBO)
	{
		logger.info("====更新数据字典信息开始====");
		logger.debug("====更新数据字典信息.入参:DictionaryBO=[{}]====", dictionaryBO);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		DictionaryPO dictionaryPO = new DictionaryPO();
		copyDictionaryBO2DictionaryPO(dictionaryBO, dictionaryPO);
		Boolean updateResult = dictionaryDao.update(dictionaryPO);
		if (updateResult)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		}
		logger.debug("====更新数据字典信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====更新数据字典信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO updateDictionary(List<DictionaryBO> dictionaryBOList) throws ConsoleException
	{
		logger.info("====更新数据字典信息列表开始====");
		logger.debug("====更新数据字典信息列表.入参:List<DictionaryBO>=[{}]====", dictionaryBOList);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		for (DictionaryBO itemBO : dictionaryBOList)
		{
			ConsoleReturnBO itemResult = updateDictionary(itemBO);
			if (ConsoleResult.FAILD.equals(itemResult.getResult()))
			{
				returnConsoleReturnBO.setResult(ConsoleResult.FAILD);
				returnConsoleReturnBO.setMessage(itemResult.getMessage());
				throw new ConsoleRollbackException();
			}
		}
		logger.debug("====更新数据字典信息列表.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====更新数据字典信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO removeDictionary(DictionaryBO dictionaryBO)
	{
		logger.info("====删除数据字典信息开始====");
		logger.debug("====删除数据字典信息.入参:DictionaryBO＝[{}]======", dictionaryBO);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		DictionaryPO dictionaryPO = new DictionaryPO();
		copyDictionaryBO2DictionaryPO(dictionaryBO, dictionaryPO);
		Boolean deleteResult = dictionaryDao.delete(dictionaryPO);
		if (deleteResult)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		}
		logger.debug("====删除数据字典信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====删除数据字典信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO removeDictionary(List<DictionaryBO> dictionaryBOList) throws ConsoleException
	{
		logger.info("====删除数据字典信息列表开始====");
		logger.debug("====删除数据字典信息列表.入参:List<DictionaryBO>＝[{}]======", dictionaryBOList);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		for (DictionaryBO itemBO : dictionaryBOList)
		{
			ConsoleReturnBO itemResult = removeDictionary(itemBO);
			if (ConsoleResult.FAILD.equals(itemResult.getResult()))
			{
				returnConsoleReturnBO.setResult(ConsoleResult.FAILD);
				returnConsoleReturnBO.setMessage(itemResult.getMessage());
				throw new ConsoleRollbackException();
			}
		}
		logger.debug("====删除数据字典信息列表.出参:ConsoleReturnBO＝[{}]====", returnConsoleReturnBO);
		logger.info("====删除数据字典信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public Long countDictionary(DictionaryBO dictionaryBO)
	{
		logger.info("====统计数据字典信息开始====");
		logger.debug("====统计数据字典信息。入参：DictionaryBO＝[{}]======", dictionaryBO);
		Long returnLong = new Long(0);
		DictionaryPO dictionaryPO = new DictionaryPO();
		copyDictionaryBO2DictionaryPO(dictionaryBO, dictionaryPO);
		returnLong = dictionaryDao.count(dictionaryPO);
		logger.debug("====统计数据字典信息.出参:Long＝[{}]======", returnLong);
		logger.info("====统计数据字典信息结束====");
		return returnLong;
	}

	private void copyDictionaryBO2DictionaryPO(DictionaryBO dictionaryBO, DictionaryPO dictionaryPO)
	{
		beanUtils.copyBeanNotNull(dictionaryBO, dictionaryPO);
		if (dictionaryBO.getSysID() != null)
		{
			dictionaryPO.setSeqID(dictionaryBO.getSysID());
		}
	}

	private void copyDictionaryPO2DictionaryBO(DictionaryPO dictionaryPO, DictionaryBO dictionaryBO)
	{
		beanUtils.copyBeanNotNull(dictionaryPO, dictionaryBO);
		if (dictionaryPO.getSeqID() != null)
		{
			dictionaryBO.setSysID(dictionaryPO.getSeqID());
		}
	}

}
